package com.aube.guide.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.aube.R;

/**
 * Created by huyaonan on 16/7/6.
 */
public class MotionGuideView extends View {

    private Bitmap mBitmap, mTips;
    private Matrix mMatrixTop, mMatrixRight, mMatrixBottom, mMatrixLeft;
    private Paint mPaint;

    private static final int PADDING = 30;

    private int counter=0;

    public MotionGuideView(Context context) {
        this(context, null);
    }

    public MotionGuideView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MotionGuideView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.guide_jt);
        mTips = BitmapFactory.decodeResource(getResources(), R.drawable.vr_tips);
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();
        int mImgWidth = mBitmap.getWidth();
        int mImgHeight = mBitmap.getHeight();

        mPaint.setAlpha(calAlpha());

        if(mMatrixTop == null) {
            mMatrixTop = new Matrix();
            mMatrixTop.reset();
            mMatrixTop.postTranslate((width - mImgWidth) / 2, PADDING);
        }
        canvas.drawBitmap(mBitmap, mMatrixTop, mPaint);

        if(mMatrixRight == null) {
            mMatrixRight = new Matrix();
            mMatrixRight.reset();
            mMatrixRight.postRotate(90);
            mMatrixRight.postTranslate(width - PADDING, (height - mImgWidth) / 2);
        }
        canvas.drawBitmap(mBitmap, mMatrixRight, mPaint);

        if(mMatrixBottom == null) {
            mMatrixBottom = new Matrix();
            mMatrixBottom.reset();
            mMatrixBottom.postRotate(180);
            mMatrixBottom.postTranslate((width - mImgWidth) / 2, height - PADDING);
        }
        canvas.drawBitmap(mBitmap, mMatrixBottom, mPaint);

        if(mMatrixLeft == null) {
            mMatrixLeft = new Matrix();
            mMatrixLeft.reset();
            mMatrixLeft.postRotate(-90);
            mMatrixLeft.postTranslate(PADDING, (height - mImgWidth) / 2);
        }
        canvas.drawBitmap(mBitmap, mMatrixLeft, mPaint);

        mPaint.setAlpha(255);
        canvas.drawBitmap(mTips, (width-mTips.getWidth())/2, height - PADDING*2 - mTips.getHeight() - mBitmap.getHeight(), mPaint);

        counter++;
        invalidate();
    }

    private int calAlpha() {
        return (int) (Math.sin(counter%100*Math.PI/100)*255);
    }

}
